package com.googlecode.lookie.platform.osgi;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.BeanNotOfRequiredTypeException;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.richclient.application.ViewDescriptor;
import org.springframework.richclient.application.ViewDescriptorRegistry;
import org.springframework.richclient.application.config.DefaultApplicationObjectConfigurer;
import org.springframework.richclient.command.config.CommandLabelConfigurable;
import org.springframework.richclient.core.ColorConfigurable;
import org.springframework.richclient.core.DescriptionConfigurable;
import org.springframework.richclient.core.LabelConfigurable;
import org.springframework.richclient.core.TitleConfigurable;
import org.springframework.richclient.image.config.ImageConfigurable;
import org.springframework.richclient.util.Assert;

public class OsgiViewDescriptorRegistry
    extends ApplicationObjectSupport
    implements ViewDescriptorRegistry
{

    private Map<String, ViewDescriptor> descriptors;

    /**
     * {@inheritDoc}
     */
    public ViewDescriptor[] getViewDescriptors()
    {
        if ( true )
        {
            Map<String, ViewDescriptor> map = new HashMap<String, ViewDescriptor>();

            Map<?, ?> beans = getApplicationContext().getBeansOfType( ViewDescriptor.class, false, false );

            for ( Object key : beans.keySet() )
            {
                String id = (String) key;
                map.put( id, (ViewDescriptor) beans.get( key ) );
            }

            DefaultApplicationObjectConfigurer configurer =
                (DefaultApplicationObjectConfigurer) getApplicationContext().getBean( "applicationObjectConfigurer" );

            Object osgiBeans = getApplicationContext().getBean( "viewDescriptors" );

            for ( Object bean : ( (List<?>) osgiBeans ) )
            {
                ViewDescriptor descriptor = (ViewDescriptor) bean;
                if (descriptor instanceof TitleConfigurable) {
                    System.out.println("TitleConfigurable");
                }

                if (descriptor instanceof LabelConfigurable) {
                    System.out.println("LabelConfigurable");
                }

                if (descriptor instanceof ColorConfigurable) {
                    System.out.println("ColorConfigurable");
                }

                if (descriptor instanceof CommandLabelConfigurable) {
                    System.out.println("CommandLabelConfigurable");
                }

                if (descriptor instanceof DescriptionConfigurable) {
                    System.out.println("DescriptionConfigurable");
                }

                if (descriptor instanceof ImageConfigurable) {
                    System.out.println("ImageConfigurable");
                }
//                configurer.configure( descriptor, descriptor.getId() );
                map.put( descriptor.getId(), descriptor );
            }

            descriptors = map;
        }

        Collection<ViewDescriptor> values = descriptors.values();
        return values.toArray( new ViewDescriptor[values.size()] );
    }

    /**
     * Returns the view descriptor with the given identifier, or null if no such bean definition with the given name
     * exists in the current application context.
     *
     * @param viewName The bean name of the view descriptor that is to be retrieved from the underlying application
     *            context. Must not be null.
     * @throws IllegalArgumentException if {@code viewName} is null.
     * @throws BeanNotOfRequiredTypeException if the bean retrieved from the underlying application context is not of
     *             type {@link ViewDescriptor}.
     */
    public ViewDescriptor getViewDescriptor( String viewName )
    {

        Assert.required( viewName, "viewName" );

        return descriptors.get( viewName );

    }
}
